package com.g2room.driving.grap.life;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.GrapErr;
import com.g2room.driving.grap.life.model.po.LifeFoodDo;
import com.g2room.driving.util.DateUtil;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapXiachufangData {
	String baseUrl = "http://www.xiachufang.com/search/?keyword=";
	String firstUrl = "http://www.xiachufang.com";
	public static void main(String[] args) {
		GrapXiachufangData cxd = new GrapXiachufangData();
		Map<String, Integer> names = cxd.getFoods();
		for(String name : names.keySet()) {
			cxd.getList(name, names.get(name));
		}
	}
	

	/**
	 * 获取头部导航菜单
	 * 
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList(String name, int id) {
		String url = "http://www.xiachufang.com/search/?keyword=" + name;
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("11");
			ge.setContent("获取链接错误");
			ge.setDescription("获取Type错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}

		if (null == doc) return;
		List<LifeFoodDo> lfds = new ArrayList<LifeFoodDo>();
		try {
			Elements els = doc.select("div.recipe-140-horizontal");
			if (els == null || els.size() == 0) return;
			for (Element el : els) {
				LifeFoodDo lfd = new LifeFoodDo();
				lfd.setFoodId(id);
				lfd.setFoodName(name);
				Elements tempEls = el.select(".cover img");
				if(null != tempEls && !tempEls.isEmpty()) {
					lfd.setCover(tempEls.first().attr("data-src"));
				}
				
				tempEls = el.select(".info .name a");
				if(null != tempEls && !tempEls.isEmpty()) {
					lfd.setSourceUrl(firstUrl + tempEls.first().attr("href"));
					lfd.setTitle(tempEls.first().text());
				}
				
				tempEls = el.select(".info .ellipsis");
				if(null != tempEls && !tempEls.isEmpty()) {
					lfd.setIng(tempEls.first().text());
				}
				
				tempEls = el.select(".info .author .gray-link");
				if(null != tempEls && !tempEls.isEmpty()) {
					lfd.setAuthor(tempEls.first().text());
				}
				lfds.add(lfd);
			}
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("11");
			ge.setContent("获取内容错误");
			ge.setDescription("获取Type错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		if(lfds.size() == 0) return;
		
		List<LifeFoodDo> nis = new ArrayList<LifeFoodDo>();
		for (LifeFoodDo ln: lfds) {
			LifeFoodDo ni = getDetail(ln);
			if(null != ni) nis.add(ni);
		}

		if(nis.size() > 0) insert2FoodDoDb(nis);
	}
	

	public LifeFoodDo getDetail(LifeFoodDo lfd) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(lfd.getSourceUrl());
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(lfd.getSourceUrl());
			ge.setType("2");
			ge.setContent("");
			ge.setDescription("获取Detail 链接错误：" + e.getMessage());
			insert2ErrDb(ge);
			return null;
		}

		Elements els = doc.select(".pure-u-2-3 div div div.cover img");
		if(null != els && !els.isEmpty()) {
			lfd.setImage(els.first().attr("src"));
		}
		els = doc.select(".pure-u-2-3 div div div.desc");
		if(null != els && !els.isEmpty()) {
			lfd.setSummary(els.first().html());
		}
		els = doc.select(".pure-u-2-3 div div div.ings");
		if(null != els && !els.isEmpty()) {
			lfd.setIngDetail(els.first().html());
		}
		els = doc.select(".pure-u-2-3 div div div.steps");
		if(null != els && !els.isEmpty()) {
			lfd.setContent(els.first().html());
		}
		els = doc.select(".pure-u-2-3 div div div.tip-container .tip");
		if(null != els && !els.isEmpty()) {
			lfd.setTips(els.first().html());
		}
		return lfd;
	}

	public void insert2FoodDoDb(List<LifeFoodDo> lfds) {
		String sql = "insert into life_food_do(content,title,ing_detail,food_name,cover,author,food_id,id,image,ing,tips,summary) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn().prepareStatement(sql);
			List<String> ids = new ArrayList<String>();
			List<String> idxNis = new ArrayList<String>();
			for(LifeFoodDo ls : lfds) {
				ps.setString(1, getStr(ls.getContent()));
				ps.setString(2, getStr(ls.getTitle()));
				ps.setString(3, getStr(ls.getIngDetail()));
				ps.setString(4, ls.getFoodName());
				ps.setString(5, ls.getCover());
				ps.setString(6, getStr(ls.getAuthor()));
				ps.setInt(7, null == ls.getFoodId() ? 0 : ls.getFoodId() );
				ps.setInt(8, null == ls.getId() ? 0 : ls.getId() );
				ps.setString(9, ls.getImage());
				ps.setString(10, getStr(ls.getIng()));
				ps.setString(11, getStr(ls.getTips()));
				ps.setString(12, getStr(ls.getSummary()));
				ps.addBatch();
				ids.add(ls.getId() + "");
				idxNis.add(JSON.toJSONString(ls, SerializerFeature.WriteDateUseDateFormat).replace("\n", ""));
			}
			ps.executeBatch();
			System.out.print("，插入数据库"+ids.size());
//			try {
//				// 插入到索引
//				new Index().createIndexBatch(ids, idxNis, IndexType.NEWS.val());
//				refreshIndex();
//			} catch (Exception e) {
//				GrapErr ge = new GrapErr();
//				ge.setType("6");
//				ge.setContent(JSON.toJSONString(nis));
//				ge.setDescription("Index错误：" + e.getMessage());
//				insert2ErrDb(ge);
//				return;
//			}
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new GrapErr();
			ge.setType("5");
			ge.setContent(JSON.toJSONString(lfds));
			ge.setDescription("insert DB 错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if (null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}
		}
	}
	

	
	private String getStr(String str) {
		try {
			str = null == str ? null : str.replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	
	public Map<String, Integer> getFoods() {
		Map<String, Integer> names = new HashMap<String, Integer>();
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			Statement statement = conn.createStatement();
			String sql = "select name,id from life_food where kind_id=1";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				names.put(rs.getString("name"), rs.getInt("id"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
//		} finally {
//			DbUtil.closeConn(conn);
		}
		return names;
	}

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn().prepareStatement("insert into grap_err(description,url,content,type,create_time) values(?,?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.setString(5, DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
			grapPs.execute();
			grapPs.close();
		} catch (Exception e) {

		} finally {
			if (null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {

				}
			}
		}
	}
}
